<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>修改密码</title>
  <style>
    body {
      font-family: 'Microsoft YaHei', Arial, sans-serif;
      background: #f5f5f5;
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100vh;
      margin: 0;
    }
    .form-container {
      background: white;
      padding: 30px;
      border-radius: 8px;
      box-shadow: 0 2px 4px rgba(0,0,0,0.1);
      width: 300px;
    }
    .form-container h2 {
      margin-bottom: 20px;
      text-align: center;
    }
    .form-group {
      margin-bottom: 15px;
    }
    .form-group label {
      display: block;
      margin-bottom: 5px;
    }
    .form-group input {
      width: 100%;
      padding: 8px;
      border: 1px solid #ccc;
      border-radius: 4px;
    }
    .btn {
      width: 100%;
      padding: 10px;
      border: none;
      border-radius: 6px;
      background: #667eea;
      color: white;
      cursor: pointer;
    }
    .btn:hover {
      background: #566ac2;
    }
  </style>
</head>
<body>
<div class="form-container">
  <h2>修改密码</h2>
  <form id="changePasswordForm">
    <div class="form-group">
      <label for="oldPassword">旧密码</label>
      <input type="password" id="oldPassword" name="oldPassword" required>
    </div>
    <div class="form-group">
      <label for="newPassword">新密码</label>
      <input type="password" id="newPassword" name="newPassword" required>
    </div>
    <div class="form-group">
      <label for="confirmPassword">确认新密码</label>
      <input type="password" id="confirmPassword" name="confirmPassword" required>
    </div>
    <button type="submit" class="btn">提交</button>
  </form>
</div>

<script>
  document.addEventListener('DOMContentLoaded', function() {
    const changePasswordForm = document.getElementById('changePasswordForm');
    changePasswordForm.addEventListener('submit', async function(event) {
      event.preventDefault();

      const oldPassword = document.getElementById('oldPassword').value;
      const newPassword = document.getElementById('newPassword').value;
      const confirmPassword = document.getElementById('confirmPassword').value;

      if (newPassword !== confirmPassword) {
        alert('新密码和确认密码不一致，请重新输入。');
        return;
      }

      const token = localStorage.getItem('token');
      if (!token) {
        window.location.href = 'login.html';
        return;
      }

      try {
        const response = await fetch('/api/user/change-password', {
          method: 'POST',
          headers: {
            'Content-Type': 'application/json',
            'Authorization': `Bearer ${token}`
          },
          body: JSON.stringify({
            oldPassword: oldPassword,
            newPassword: newPassword,
            confirmPassword: confirmPassword // 添加此字段
          })
        });

        let result;
        if (response.ok) {
          result = await response.json();
          alert('密码修改成功，请重新登录。');
          localStorage.removeItem('token');
          window.location.href = 'login.html';
        } else {
          try {
            result = await response.json();
            alert('密码修改失败：' + (result.message || '未知错误'));
          } catch (jsonError) {
            const errorText = await response.text();
            alert('密码修改失败：' + errorText);
          }
        }
      } catch (error) {
        console.error('请求出错：', error);
        alert('请求出错，请稍后重试。');
      }
    });
  });
</script>
</body>
</html>